package com.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manage.common.util.PageUtils;
import com.manage.sys.entity.SysMenu;
import com.manage.sys.mapper.SysMenuMapper;
import com.manage.sys.model.SysMenuQueryDTO;
import com.manage.sys.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author 波哥
 * @since 2022-11-18
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public PageUtils ListPage(SysMenuQueryDTO dto) {
        //先查询所有的一级 英单的数据，分页是针对一 级英单的数据分页
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>() ;
        wrapper.eq("parent_id", 0)
        .like(StringUtils.isNotEmpty(dto.getName()),"name" , dto. getName())
        .orderByAsc("order_num");
        Page<SysMenu> page = this. page (dto. page(), wrapper);
        //查询出该一级莫单对应的二级菜单
        List<SysMenu> list = page. getRecords();
        List<SysMenu> menus = list . stream() .map(item -> {
            Long menuId = item. getMenuId();
            //根据menuId 查询出所有的二级奖单
            QueryWrapper<SysMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id" , menuId)
                    .orderByAsc("order_num");
            List<SysMenu> subMenus = this. baseMapper.selectList(wrapper1);
            item.setChildren(subMenus);
            return item;
        }) . collect(Collectors.toList());
        page . setRecords (menus);

        return new PageUtils(page) ;

    }
    @Override
    public List<SysMenu> getList() {
        //先查询所有的一级 英单的数据，分页是针对一 级英单的数据分页
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>() ;
        wrapper.eq("parent_id", 0);
        List<SysMenu> sysMenus = this.list(wrapper);
        //查询出该一级莫单对应的二级菜单
        List<SysMenu> menus = sysMenus . stream() .map(item -> {
            Long menuId = item. getMenuId();
            //根据menuId 查询出所有的二级奖单
            QueryWrapper<SysMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id" , menuId);
            List<SysMenu> subMenus = this. baseMapper.selectList(wrapper1);
            item.setChildren(subMenus);
            return item;
        }) . collect(Collectors.toList());

        return menus ;

    }
}
